<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>定义Promise类then函数</title>
  <script src="./promise.js"></script>
</head>
<body>
  <ul>
    <li>只有通过resolve或reject改变状态时，对应then函数才会执行</li>
    <li>换言之，只有Promise状态为resolved时，才会执行then函数的成功回调onResolved才执行</li>
    <li>只有Promise状态为rejected时，才会执行then函数的错误回调onRejected才执行</li>
    <li>then函数回调onResolved或onRejected不需要同时传或都是可选传</li>
    <li>then函数回调onResolved或onRejected中出现异常需要通过try...catch捕获</li>
    <li>then函数回调onResolved或onRejected是异步的，但目前仍然是同步(未解决)</li>
  </ul>
  <script>
    // success -> main
    const promise = new MyPromise((resolve, reject) => {
      resolve('success')
      // reject('fail')
    }).then(res => {
      console.log(res)
      console.log(a) // 异常
    }, err => console.log(err))
    console.log('main')
  </script>
</body>
</html>